<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-friendadd"></i>平台数据-文章数据
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <div class="inline_div">
          <el-button type="primary" @click="ALL" class="lkx_r20">全部</el-button>
          <el-input
            v-model.trim="select_word"
            placeholder="输入文章名称或者ID"
            class="handle-input mr10"
            @keyup.enter.native="search_key(select_word)"
          ></el-input>
          <el-button type="primary" icon="search" @click="search_key(select_word)">查询</el-button>
        </div>
        <div class="inline_div">
          <el-input
            v-model.trim="media_key"
            placeholder="输入发布账号"
            class="handle-input mr10"
            @keyup.enter.native="adminkey(media_key)"
          ></el-input>
          <el-button type="primary" icon="search" @click="adminkey(media_key)">查询</el-button>
        </div>
      </div>
      <el-table
        border
        ref="singleTable"
        :data="tableData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column type="index" width="50" label="序号" align="center"></el-table-column>
        <el-table-column prop="news_name" label="文章名称" align="center"></el-table-column>
        <el-table-column prop="news_id" label="文章ID" align="center"></el-table-column>
        <el-table-column prop="news_class" label="文章分类" align="center"></el-table-column>
        <el-table-column prop="media_name" label="发布账号" align="center"></el-table-column>
        <el-table-column label="发布时间" align="center">
          <template slot-scope="scope">{{ scope.row.release_time }}</template>
        </el-table-column>
        <el-table-column prop="app_views" label="APP点击量" align="center"></el-table-column>
        <el-table-column prop="web_views" label="站外点击量" align="center"></el-table-column>
        <el-table-column prop="shares_num" label="分享数量" align="center"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-lx-rankfill"
              @click="handleEdit(scope.$index, scope.row)"
            >查看走势</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination_div">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="Totals"
          :page-count="pageTotal"
          :page-size="10"
          :current-page.sync="currPage"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import * as api from "../../http/api.js";
export default {
  data() {
    return {
      //表格数据
      tableData: [],
      select_word: "", //搜索的文章ID和文章名称关键词的默认值
      media_key: "", //发布账号里面关键词的默认值
      Token: sessionStorage.getItem("Token"),
      currPage: 1, //当前分页
      Totals: 1, //总条数 默认为1条，
      pageTotal: 2, //总页数！
      article_id: "" //id 用来连接详情的参数
    };
  },
  methods: {
    getData(key, mediaKey) {
      //获得表格数据！
      this.$post(api.news_statistics, {
        token: this.Token,
        page: this.currPage,
        news: key,
        media: mediaKey
      }).then(res => {
        if (res.status == "0") {
          this.tableData = res.data.map((val, index, arr) => {
            let jsons = [];
            jsons.news_name = val.news_name;
            jsons.news_id = val.news_id;
            jsons.news_class = val.news_class;
            jsons.media_name = val.media_name;
            jsons.release_time = val.release_time; //发布时间
            jsons.app_views = val.app_views;
            jsons.web_views = val.web_views;
            jsons.shares_num = val.shares_num;
            return jsons;
          });
          // 获取分页数据
          this.Totals = res.totals;
          this.pageTotal = res.pageTotal;
          this.currPage = res.currPage;
        } else {
         console.log(res.msg);
        }
        // 如果登录过期
        if (res.status == "relogin") {
          this.$message.error(res.msg);
          this.$router.push("/login");
        }
      }).catch(error=>{
        console.log(error);
      })
    },
    //搜索关键词
    search_key(select_word) {
      if (select_word == "") {
        this.$message.error(`搜索不能为空,请搜索文章名称或ID`);
        return;
      } else {
        this.currPage = 1;
        this.getData(this.select_word, this.media_key);
      }
    },
    adminkey(adkey) {
      if (adkey == "") {
        this.$message.error(`搜索不能为空,请搜索发布账号`);
        return;
      } else {
        this.currPage = 1;
        this.getData("",adkey);
      }
    },
    //查看详情页面
    handleEdit(index, row) {
      //点击进去详情
      this.article_id = this.tableData[index].news_id;
      this.$router.push({
        path: "/articleTrend",
        query: { id: this.article_id }
      });
    },
    //点击分页
    handleCurrentChange(val) {
      this.currPage = val;
      this.getData(this.select_word,this.media_key);
    },
    ALL() {
      this.currPage=1;
      this.select_word="";
      this.media_key="";
      this.getData(this.select_word,this.media_key);
    }
  },

  created() {
    this.getData(this.select_word, this.media_key);
  }
};
</script>
<style scoped>
.mr10 {
  margin-right: 10px;
}
.handle-input {
  width: 250px;
  display: inline-block;
}
.handle-box {
  padding-bottom: 20px;
}
.inline_div {
  display: inline-block;
  margin-right: 10px;
}
.zh_status {
  line-height: 32px;
  margin-left: 20px;
}
.status_select {
  margin: 0 10px;
}
.btn_right {
  float: right;
}
.pagination_div {
  margin-top: 20px;
  text-align: right;
}
</style>